var upColor = '#ec0000';
var downColor = '#00da3c';
var begin = "";
var end = "";
var stockData;


function getUrlParam(code) {
    const url = new URL(window.location.href);
    const params = new URLSearchParams(url.search);
    return params.get(code);
}


function splitData(rawData) {
    var quotaName = getUrlParam("quota");
    let stockDates = [];
    let values = [];
    let volumes = [];
    let marks = [];
    let quotas = [];
    for (let i = 0; i < rawData.length; i++) {
        var data = rawData[i];
        if (i == 0) {
            begin = data.stockDate;
        }
        end = data.stockDate;
        stockDates.push(data.stockDate);
        values.push(data.value);
        quotas.push(data.quota[quotaName]);
        volumes.push([i, data.volume, data.upOrDown == 'y' ? 1 : -1]);
        if (data.mark) {
            var yPosition = data.data.closePrice * 1.03.toFixed(2);
            marks.push({coord: [i, yPosition], value: data.mark});
        }

    }
    return {
        data: rawData,
        stockDates: stockDates,
        values: values,
        volumes: volumes,
        marks: marks,
        quotas: quotas
    };
}

function calculateMA(dayCount, data) {
    var result = [];
    for (var i = 0, len = data.values.length; i < len; i++) {
        if (i < dayCount) {
            result.push('-');
            continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
            sum += data.values[i - j][1];
        }
        result.push(+(sum / dayCount).toFixed(3));
    }
    return result;
}


function jumpNext() {
    index = index + 1;
    var url = getRequestUrl();
    $("#index").val(index);
    loadChart(url);
}

function jumpBefore() {
    index = index - 1;
    var url = getRequestUrl();
    $("#index").val(index);
    loadChart(url);
}